<!-- xml version="1.0" encoding="utf-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
	<title>Latin Exercises</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="../classics.css" rel="stylesheet" type="text/css">
</head>
<?php

try {
	$db = new PDO('sqlite:keg.db3');
	$q = $db->query('SELECT DISTINCT number FROM exercises ORDER BY number;');
	$nums = array();
	foreach( $q->fetchAll() as $row) {
		$nums[$row['number']] = array();
	}
	
	foreach($nums as $num => $alib) {
		$q = $db->query("SELECT DISTINCT type FROM exercises WHERE number=$num ORDER BY type DESC;");
		foreach( $q->fetchAll() as $row) {
			$nums[$num][] = $row['type'];
		}
	}

	$db = NULL;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

// A function to return the Roman Numeral, given an integer
function numberToRoman($num) {
	// Make sure that we only use the integer portion of the value
	$n = intval($num);
	$result = '';

	// Declare a lookup array that we will use to traverse the number:
	$lookup = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
					'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
					'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);

	foreach ($lookup as $roman => $value) {
		// Determine the number of matches
		$matches = intval($n / $value);

		// Store that many characters
		$result .= str_repeat($roman, $matches);

		// Substract that from the number
		$n = $n % $value;
	}

	// The Roman numeral should be built, return it
	return $result;
}
?>


<body>


<div id="container">
<div id="center">
<form name="v01" action="exFlash.php" method="post">
<table align="center">
<caption>Exercises:</caption>
<?php
$n = 0;
foreach($nums as $num => $alib) {
	$nr = numberToRoman($num);
	print "<tr>";
	foreach($alib as $type) {
		print "<td><input type=\"checkbox\" name=\"numtypes[]\" value=\"$num.$type\" />$nr - $type</td>";
	}
	if (! ($n % 3)) {
		print "</tr>\n<tr>";
	}
	$n++;
	print "</tr>\n";
}
?>
<tr class="separate-above">
	<td align="center" colspan=10><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</div>
</div>

</body>
</html>
